package com.hcbd.tdwt.ui.fuda.common.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.bd.BDNotificationManager;
import com.hcbd.tdwt.ui.fuda.common.activity.PositionInfoActivity_;
import com.hcbd.tdwt.ui.fuda.common.activity.ReportPeaceActivity_;
import com.hcbd.tdwt.ui.fuda.common.activity.SendMsgActivity_;
import com.hcbd.tdwt.ui.fuda.common.activity.SosActivity_;
import com.hcbd.tdwt.ui.fuda.common.activity.StatusPageActivity_;
import com.hcbd.tdwt.ui.fuda.common.activity.WeatherHelperActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.AboutUsActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.AuthInfoActivity_;

import com.hcbd.tdwt.ui.fuda.setting.activity.ChannelSetActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.HelpCenterActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.PositionReportingActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.PrivacyProtocolActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.ServiceProtocolActivity_;
import com.hcbd.tdwt.ui.fuda.setting.activity.SunriseAndSunsetActivity_;

import com.hcbd.tdwt.base.AuthManger;
import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.GlobalParams;
import com.pangu.bdsdk2021.entity.terminalthree.BDSXXInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDSnrInfo;


import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.MessageFormat;
import java.util.Objects;

@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_my_fuda)
public class MyFragment extends BaseFragment {
    @ViewById
    TextView title;
    @ViewById
    ImageView headLeft;
    @ViewById
    TextView contentTv,cardNumTv,frequencyTV,signalLockTv,signalTv,activationTV,account,contentInfo;
    @ViewById
    LinearLayout bdInfoLL,deviceLL;
    final private int refresh_code=5;
    private Drawable brownGreenBg;
    private Drawable scarletBg;

    @AfterViews
    protected void init() {
        title.setText("我的");
        brownGreenBg=getContext().getDrawable(R.drawable.ova_brown_green_bg);
        scarletBg=getContext().getDrawable(R.drawable.ova_scarlet_bg);
        headLeft.setVisibility(View.GONE);
        if(AuthManger.getAuthManger().isPicOnline()&&AuthManger.getAuthManger().isVoiceOnline()){
            activationTV.setText("已激活");
            activationTV.setTextColor(ContextCompat.getColor(getContext(),R.color.white));
        }else{
            activationTV.setText("未激活");
            activationTV.setTextColor(ContextCompat.getColor(getContext(),R.color.colorPrimary));
        }
        initMsgRegister();
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDSnrInfo bdSnrInfo){
        updateBSIData();
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDSXXInfo bdsxxInfo){
        refreshStatus();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    public void refreshStatus(){
        if(BeidouBoxParams.isBoxConnectNormal){
            contentTv.setText("已连接");
            contentTv.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.layout_frame_brown_green));
            cardNumTv.setText(MessageFormat.format("北斗卡号：{0}", BeidouBoxParams.userCardNumber));
            frequencyTV.setText(MessageFormat.format("通信频度：{0}", BeidouBoxParams.sendFreq));
            account.setText(String.format("%s", BeidouBoxParams.userCardNumber));
            contentInfo.setText("关闭北斗服务");
            contentInfo.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.layout_frame_brown_green));
        }else{
            contentTv.setText("未连接");
            contentTv.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.layout_frame_scarlet));
            cardNumTv.setText(MessageFormat.format("北斗卡号：{0}", "-"));
            frequencyTV.setText(MessageFormat.format("通信频度：{0}", "-"));
            account.setText("--");
            contentInfo.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.layout_frame_scarlet));
            contentInfo.setText("打开北斗服务");
        }


    }
    public void updateBSIData(){
        if(BeidouBoxParams.isBoxConnectNormal){
            signalLockTv.setText("锁定");
            signalLockTv.setBackground(brownGreenBg);
            if(BeidouBoxParams.getBdSignal()>2){
                signalTv.setBackground(brownGreenBg);
                signalTv.setText("强");
            }else{
                signalTv.setBackground(scarletBg);
                signalTv.setText("弱");
            }
        }else{
            signalLockTv.setText("无");
            signalTv.setText("无");
            signalLockTv.setBackground(scarletBg);
            signalTv.setBackground(scarletBg);
        }

    }
    @Click
    void commandCenterLL(){
       Intent intent=new Intent();
        intent.setClass(getContext(), SendMsgActivity_.class);
        intent.putExtra("receiverNumber", GlobalParams.rdCentreNumber);
        intent.putExtra("receiverName","指挥中心");
        intent.putExtra("entryMode", Constant.ENTRY_SEND_CENTER_NUMBER);
        startActivityForResult(intent,refresh_code);
    }

    @Click
    void sendMsgLL(){
        Intent intent=new Intent();
        intent.setClass(Objects.requireNonNull(getContext()), SendMsgActivity_.class);
        startActivityForResult(intent,refresh_code);
    }
    @Click
    void sosLL(){
        Intent intent=new Intent();
        intent.setClass(Objects.requireNonNull(getContext()), SosActivity_.class);
        startActivity(intent);
    }
    @Click
    void reportPeaceLL(){
        Intent intent=new Intent();
        intent.setClass(Objects.requireNonNull(getContext()), ReportPeaceActivity_.class);
        startActivity(intent);
    }
    @Click
    void positionReportLL(){
        Intent intent=new Intent();
        intent.setClass(Objects.requireNonNull(getContext()), PositionReportingActivity_.class);
        startActivity(intent);
    }
    @Click
    void satelliteLL(){
        Intent intent=new Intent(getContext(), PositionInfoActivity_.class);
        startActivity(intent);
    }
    @Click
    void weatherAssistantLL(){
        Intent intent=new Intent(getContext(), WeatherHelperActivity_.class);
        startActivity(intent);
    }
    @Click
    void authInfoRL(){
        Intent intent=new Intent(getContext(), AuthInfoActivity_.class);
        startActivity(intent);
    }
    @Click
    void offMapLL(){
        startActivity(new Intent(this.getContext(), com.amap.api.maps.offlinemap.OfflineMapActivity.class));
    }
    @Click
    void sunriseLL(){
        Intent intent=new Intent(getContext(), SunriseAndSunsetActivity_.class);
        startActivity(intent);
    }
    @Click
    void channelSetLL(){
        Intent intent=new Intent(getContext(), ChannelSetActivity_.class);
        startActivity(intent);
    }
    @Click
    void bdAgreeLL(){

    }
    @Click
    void helpCenterLL(){
        Intent intent=new Intent(getContext(), HelpCenterActivity_.class);
        startActivity(intent);
    }
    @Click
    void privacyAgreeLL(){
        Intent intent=new Intent(getContext(), PrivacyProtocolActivity_.class);
        startActivity(intent);
    }
    @Click
    void serviceAgreeLL(){
        Intent intent=new Intent(getContext(), ServiceProtocolActivity_.class);
        startActivity(intent);
    }
    @Click
    void aboutLL(){
        Intent intent=new Intent(getContext(), AboutUsActivity_.class);
        startActivity(intent);
    }
    @Click
    void bdInfoLL(){

    }
    @Click
    void accountLL(){
        if(BeidouBoxParams.isBoxConnectNormal){
            BDNotificationManager.getInstance().clsoeBdcom();
            refreshStatus();
            updateBSIData();
        }else{
            BDNotificationManager.getInstance().openBdcom(getActivity());
        }
    }
    @Click
    void signalLockTv(){

    }
    @Click
    void deviceLL(){
        Intent intent=new Intent(getContext(), StatusPageActivity_.class);
        startActivity(intent);

    }

}
